package com.cgbl.cg.vm.project

import android.app.Application
import androidx.databinding.ObservableField
import api.ProjectServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.PageListBean
import com.robot.base.model.ProjectBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ProjectMenuViewModel(application: Application) : GLBaseViewModel(application) {
    val typeName = ObservableField<String>()
    var categoryId = ""
    val list = arrayListOf<CourseAllTypeBean>()
    val uc = UiChangeEvent()
    var page = 1

    class UiChangeEvent {
        val selectType: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val projectEvent = SingleLiveEvent<List<ProjectBean>>()
        val refreshEvent = SingleLiveEvent<Int>()
    }

    val type = ObservableField<Int>(0)

    val tag = ObservableField(true)

    val leftTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(true)
    })

    val rightTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(false)
    })
    val showDialogCommand = BindingCommand<Any>(BindingAction {
        uc.selectType.call()
    })

    fun refresh() {
        page = 1
        casePage()
    }

    fun load() {
        page++
        casePage()
    }

    fun initData() {
        allType()
    }

    fun allType() {
        ProjectServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                    list.addAll(t.data)
                    if (!t.data.isNullOrEmpty()) {
                        categoryId = t.data[0].id ?: ""
                        typeName.set(t.data[0].name)
                        casePage()
                    }
                }
            })
    }

    fun casePage() {
        ProjectServiceFactory.casePage(
            mapOf(
                "pageNo" to page,
                "pageSize" to 10,
                "categoryId" to categoryId,
                "sort" to if (tag.get() == true) {
                    0
                } else {
                    1
                }
            )
        )
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<PageListBean<ProjectBean>>>(this) {
                override fun onNext(t: BaseResponse<PageListBean<ProjectBean>>) {
                    uc.projectEvent.postValue(t.data.list)
                    uc.refreshEvent.postValue(
                        if (t.data.total.toInt() < page * 10) {
                            0
                        } else {
                            1
                        }
                    )
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(
                        0
                    )
                }
            })
    }
}